package com.yzx.lifeassistants.model.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;

import com.yzx.lifeassistants.bean.PartTimeInfo;
import com.yzx.lifeassistants.model.IQueryPartTime;
import com.yzx.lifeassistants.model.callback.IQueryPartTimeCB;
import com.yzx.lifeassistants.utils.HttpClientUtil;
import com.yzx.lifeassistants.utils.IHttpCallBack;
import com.yzx.lifeassistants.utils.LogcatUtils;
import com.yzx.lifeassistants.utils.PropertyUtil;

/**
 * @Description: 查询兼职信息
 * @author: yzx
 * @time: 2016-3-17 下午8:57:18
 */
public class QueryPartTimeImpl implements IQueryPartTime {
	public static final String TAG = "QueryPartTimeImpl";
	private String refererUrl;// URL
	private IQueryPartTimeCB queryCallBack;// 回调

	public QueryPartTimeImpl(Context context, IQueryPartTimeCB queryCallBack) {
		this.queryCallBack = queryCallBack;
		Properties properties = PropertyUtil.getProperties(context,
				"base.properties");
		refererUrl = properties.getProperty("part_time_referer_url");
	}

	/**
	 * @Description: 查询兼职信息
	 */
	@Override
	public void queryPartTime(int page, String place) {
		String url = this.refererUrl + place + "/index" + page + ".html";
		HttpClientUtil.sendRequest(url, null, new IHttpCallBack() {

			@Override
			public void onFinish(String response) {
				LogcatUtils.i(response);
				if (null == response || "".equals(response)) {
					LogcatUtils.e("获取数据失败");
					queryCallBack.queryError(IQueryPartTimeCB.RESULR_NULL);
				} else {
					List<PartTimeInfo> partTimeInfos = new ArrayList<PartTimeInfo>();
					Document doc = Jsoup.parse(response);
					Elements elements = doc.select("div.joblist>ul>li.hover");
					for (Element element : elements) {
						PartTimeInfo partTimeInfo = new PartTimeInfo();
						String time = element.select("span.listzptime").first()
								.text();
						LogcatUtils.i(">>>>>>>>>>>" + time);
						if (haveData(time)) {
							partTimeInfo.setTime(time.replaceAll("\\s*", ""));
						}
						String url = element.select("span.jobtitle>a").first()
								.attr("href");
						LogcatUtils.i(">>>>>>>>>>>>" + url);
						if (haveData(url)) {
							partTimeInfo.setUrl(url);
						}
						String title = element.select("span.jobtitle>a")
								.first().text();
						LogcatUtils.i(">>>>>>>>>>>>" + title);
						if (haveData(title)) {
							partTimeInfo.setTitle(title);
						}
						String company = element.select("span.listcompany")
								.first().toString();
						LogcatUtils.i(">>>>>>>>>>>>" + company);
						if (haveData(company)) {
							if (company.contains("href")) {
								String companyName = element
										.select("span.listcompany>a").first()
										.text();
								LogcatUtils.i(">>>>>>>>>>>>" + companyName);
								if (haveData(companyName)) {
									partTimeInfo.setCompany(companyName);
									partTimeInfo.setIsVerify(true);
								}
							} else {
								String companyName = element
										.select("span.listcompany").first()
										.text();
								LogcatUtils.i(">>>>>>>>>>>>" + companyName);
								if (haveData(companyName)) {
									partTimeInfo.setCompany(companyName);
									partTimeInfo.setIsVerify(false);
								}
							}
						}
						if (haveData(partTimeInfo.getTime())
								&& haveData(partTimeInfo.getUrl())
								&& haveData(partTimeInfo.getTitle())
								&& haveData(partTimeInfo.getCompany())) {
							partTimeInfos.add(partTimeInfo);
						}
					}
					if (null == partTimeInfos || 1 > partTimeInfos.size()) {
						LogcatUtils.e("获取数据失败");
						queryCallBack.queryError(IQueryPartTimeCB.RESULR_NULL);
					} else {
						LogcatUtils.i("获取数据成功");
						queryCallBack.querySuccess(partTimeInfos);
					}
				}
			}

			@Override
			public void onError(Exception e) {
				if (null != e.getMessage()) {
					LogcatUtils.e(TAG, e.getMessage());
				}
				queryCallBack.queryError(IQueryPartTimeCB.QUERY_ERROR);
			}
		});
	}

	/**
	 * 
	 * @Description: 是否有数据
	 */
	private Boolean haveData(String data) {
		if (null == data || "".equals(data)) {
			return false;
		} else {
			return true;
		}
	}
}
